<template>
  <div>
    <Panel>
      <span slot="title">添加账号</span>

      <div slot="main">
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <!-- 账号 -->
          <el-form-item label="账号" prop="account">
            <el-input
              type="text"
              v-model="ruleForm.account"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item label="密码" prop="password">
            <el-input
              type="password"
              v-model="ruleForm.password"
              autocomplete="off"
            ></el-input>
          </el-form-item>

          <el-form-item label="用户组" prop="userGroup">
            <el-select v-model="ruleForm.userGroup" placeholder="请选择用户组">
              <el-option label="普通用户" value="普通管理员"></el-option>
              <el-option label="超级用户" value="超级管理员"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >确定</el-button
            >
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </Panel>
  </div>
</template>

<script>
import Panel from "@/components/panel.vue";
import { checkAcc, checkPass } from "@/utils/validate.js";
import { accaddApi, accApi } from "@/api/user";
export default {
  data() {
    var validateAcc = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入用户名"));
      } else if (!checkAcc(value)) {
        callback(new Error("请输入2-14位用户名"));
      } else {

        accaddApi({
          account: value,
        }).then((res) => {
          //console.log(res.data);
          let { code, msg } = res.data;
          if (code == 200) {
            callback();
          } else {
            callback(new Error(msg));
          }
        });
        
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else if (!checkPass(value)) {
        callback(new Error("请输入 6 到 12 位密码"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        account: "",
        password: "",
        userGroup: "",
      },
      rules: {
        account: [{ validator: validateAcc, trigger: "blur" }],
        password: [{ validator: validatePass, trigger: "blur" }],
        userGroup: [
          { required: true, message: "请选择用户组", trigger: "change" },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          // 填写ajax
          // console.log(this.ruleForm);
          // alert("填写ajax");

          let res = await accApi(this.ruleForm);
          //console.log(res);
          let { code, msg } = res.data;
          if (code == 0) {
            // this.$message.success(msg);
            this.$router.push('/account/AccList')
          }

          // accApi(this.ruleForm).then((res) => {
          //   console.log(res.data);
          //   let { code, msg } = res.data;
          //   if (code == 0) {
          //     this.$message.success(msg);
          //   } else {
          //     this.$message.error(msg);
          //   }
          // });
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
  components: {
    Panel,
  },
};
</script>

<style lang="less" scoped>
/deep/.el-input {
  width: 300px;
}
</style>